package com.yulamg.core.controller;

import com.yulamg.core.respository.UserRespository;
import com.yulamg.core.service.UserService;
import com.yulamg.core.user.User;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {


    @Autowired
    private UserService service;

    @Autowired
    private HttpServletRequest request;


    @PostMapping
    public void create(@RequestBody User user){
        service.create(user);
    }

    @PutMapping("/{id}")
    public void update(User user,@PathVariable String id){
        service.update(user,id);
    }

    @GetMapping("/{id}")
    public User get(@PathVariable String id){
        User user = (User)request.getAttribute("user");
        if(user==null){
            throw new RuntimeException("未登录不能获取相关的信息");
        }
        return service.get(id);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id){
        service.delete(id);
    }

    @GetMapping("/query")
    public List<User> list(String name){
        return service.list(name);
    }

}
